<?php
session_start();
require_once __DIR__.'/../../utils/checkTokenUtils.php';
require_once __DIR__.'/../../model/BookModel.php';
use model\BookModel;
use utils\MysqlDBUtils;

$dbUtil = new MysqlDBUtils();

// 读取Session中的提示信息
$successMessage = $_SESSION['success_message'] ?? null;
$errorMessage = $_SESSION['error_message'] ?? null;

// 初始化模型并获取数据
$bookModel = new BookModel();
$q = isset($_GET['q']) ? trim($_GET['q']) : '';
$currentPage = max(1, intval($_GET['page'] ?? 1));
$perPage = max(1, min(intval($_GET['per_page'] ?? 8), 100));
$paginationData = $bookModel->getPaginatedBooks($q, $currentPage, $perPage);

// 提取分页数据为变量
extract($paginationData);

$title = '图书列表';
$description = '管理系统中的所有图书信息';
ob_start();
?>

    <!-- 成功/错误提示 -->
    <?php if ($successMessage): ?>
        <div id="successNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-100 border border-green-400 text-green-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
            <?= htmlspecialchars($successMessage) ?>
        </div>
    <?php endif; ?>

    <?php if ($errorMessage): ?>
        <div id="errorNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
            <?= htmlspecialchars($errorMessage) ?>
        </div>
    <?php endif; ?>

    <!-- 清除已显示的消息 -->
    <?php
    unset($_SESSION['success_message'], $_SESSION['error_message']);
    ?>

    <!-- 页面专属内容 -->
    <div class="mb-4 flex gap-2 items-center justify-between">
        <button id="addBookBtn"  class="add-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors ">新增图书</button>
        <!-- 模糊搜索表单 -->
        <?php include __DIR__.'/partials/search_form.php'; ?>
    </div>

    <table class="w-full">
        <thead>
        <tr class="bg-gray-100">
            <th class="p-3">ISBN</th>
            <th class="p-3">书名</th>
            <th class="p-3">作者</th>
            <th class="p-3">出版社</th>
            <th class="p-3">出版日期</th>
            <th class="p-3">价格</th>
            <th class="p-3">库存</th>
            <th class="p-3">操作</th>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($books as $book): ?>
            <tr class="border-b">
                <input
                    type="hidden"
                    name="id"
                    value="<?= htmlspecialchars($book['id'])?>"
                >
                <td class="p-3 text-center"><?php echo htmlspecialchars($book['isbn']); ?></td>
                <td class="p-3 text-center"><?php echo htmlspecialchars($book['title']); ?></td>
                <td class="p-3 text-center"><?php echo htmlspecialchars($book['author']); ?></td>
                <td class="p-3 text-center"><?php echo htmlspecialchars($book['publisher']); ?></td>
                <td class="p-3 text-center"><?php echo $book['publish_date']; ?></td>
                <td class="p-3 text-center"><?php echo $book['price']; ?></td>
                <td class="p-3 text-center"><?php echo $book['stock']; ?></td>
                <td class="p-3 text-center">
                    <button
                            class="edit-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors mr-2"
                            data-id="<?= htmlspecialchars($book['id'])?>"
                            data-isbn="<?= htmlspecialchars($book['isbn']) ?>"
                            data-title="<?= htmlspecialchars($book['title']) ?>"
                            data-author="<?= htmlspecialchars($book['author']) ?>"
                            data-publisher="<?= htmlspecialchars($book['publisher']) ?>"
                            data-publish-date="<?= $book['publish_date'] ?>"
                            data-price="<?= $book['price'] ?>"
                            data-stock="<?= $book['stock'] ?>"
                    >
                        编辑
                    </button>
                    <button class="delete-btn px-3 py-1 bg-red-500 text-white rounded-md hover:bg-red-600 transition-colors" data-delete-url="DeleteBook.php?id=<?= htmlspecialchars($book['id'])?>">删除</button>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>


<!-- 分页控件 -->
<?php include __DIR__ . '/../partials/pagination.php'; ?> <!-- 引入分页模板 -->
<?php
$content = ob_get_clean();
include __DIR__ . '/../layouts/Main.php'; // 引入主布局
// 引入删除确认模态框
include __DIR__ . '/../partials/del_modal.php';
?>

    <!-- 引入编辑模态框模板 -->
<?php include __DIR__.'/partials/edit_modal.php'; ?>
<!-- 引入添加图书模态框模板 -->
<?php include __DIR__.'/partials/add_modal.php';?>

<script>
function deleteBook(bookId) {
    if (confirm('确定要删除这本书吗？')) {
        const form = document.createElement('form');
        form.method = 'post';
        form.action = 'DeleteBook.php';

        const idInput = document.createElement('input');
        idInput.type = 'hidden';
        idInput.name = 'id';
        idInput.value = bookId;
        form.appendChild(idInput);

        document.body.appendChild(form);
        form.submit();
    }
}
</script>

